# -*- coding: utf-8 -*-
"""Item models."""
import datetime as dt
from AmoluoSale.database import Column, Model, SurrogatePK, db

class Item(SurrogatePK, Model):
    __tablename__ = 'items'
    def __init__(self):
        self.start_time = dt.datetime.now()
        self.end_time = dt.datetime.now()+dt.timedelta(days = 1)

    title = Column(db.String(128), nullable=False)
    pre_price = Column(db.Float(), nullable=False)
    now_price = Column(db.Float(), nullable=False)
    start_time = Column(db.DateTime, nullable=False)
    end_time = Column(db.DateTime, nullable=False)
    total = Column(db.Integer, nullable=False)#总量
    # inventory = Column(db.Integer, nullable=False)#库存
    sold = Column(db.Integer, nullable=False)#已售
    picUrl = Column(db.String(512), nullable=True)
    desc = Column(db.String(4096), nullable=False)

    @property
    def inventory(self):
        return self.total - self.sold if self.total - self.sold > 0 else 0

    def as_dict(self):
       return {c.name: getattr(self, c.name) for c in self.__table__.columns}

class Order(SurrogatePK, Model):
    __tablename__ = 'orders'

    def __init__(self):
        self.created_at = dt.datetime.now()

    openid = Column(db.String(512), nullable=False)
    addr_id = Column(db.Integer, nullable=False)
    nickname = Column(db.String(512), nullable=False)
    sex = Column(db.String(8), nullable=False)
    province = Column(db.String(128), nullable=True)
    city = Column(db.String(128), nullable=True)
    country = Column(db.String(128), nullable=True)
    item_id = Column(db.Integer, nullable=False)
    quantity = Column(db.Integer, nullable=False)
    created_at = Column(db.DateTime, nullable=False)
    bank_type = Column(db.String(64), nullable=True)
    fee_type = Column(db.String(64), nullable=True)
    result_code = Column(db.String(64), nullable=True)
    bank_type = Column(db.String(64), nullable=True)
    cash_fee = Column(db.Integer, nullable=True)
    total_fee = Column(db.Integer, nullable=True)
    transaction_id = Column(db.String(128), nullable=True)
    out_trade_no = Column(db.String(128), nullable=True)
    status = Column(db.Integer, default=1,nullable=True) #1为待发货，2为已发货，3为已完成
    express_no = Column(db.String(128), nullable=True)#快递单号
    express_company = Column(db.String(128), nullable=True)#快递公司
    delivered_at = Column(db.DateTime, nullable=True)#发货时间

    def as_dict(self):
       return {c.name: getattr(self, c.name) for c in self.__table__.columns}

class Address(SurrogatePK, Model):
    __tablename__ = 'address'
    def __init__(self):
        self.created_at = dt.datetime.now()
    openid = Column(db.String(512), nullable=False)
    userName = Column(db.String(128), nullable=False)
    telNumber = Column(db.String(64), nullable=False)
    addressPostalCode = Column(db.String(64), nullable=False)
    proviceFirstStageName = Column(db.String(64), nullable=False)
    addressCitySecondStageName = Column(db.String(64), nullable=False)
    addressCountiesThirdStageName = Column(db.String(64), nullable=False)
    addressDetailInfo = Column(db.String(512), nullable=False)
    created_at = Column(db.DateTime, nullable=False)
    def as_dict(self):
       return {c.name: getattr(self, c.name) for c in self.__table__.columns}

class Admin(SurrogatePK, Model):
    __tablename__ = 'admin'

    openid = Column(db.String(512), nullable=False)
    note = Column(db.String(512), nullable=True)


class Reserve_Order(SurrogatePK, Model):
    __tablename__ = 'reserve_order'
    def __init__(self):
        self.created_at = dt.datetime.now()
        self.expire_at = dt.datetime.now()+dt.timedelta(minutes = 5)
    openid = Column(db.String(512), nullable=False)
    item_id = Column(db.Integer, nullable=False)
    quantity = Column(db.Integer, nullable=False)
    created_at = Column(db.DateTime, nullable=False)
    expire_at = Column(db.DateTime, nullable=False)
    addr_id = Column(db.Integer, nullable=True)
    task_id = Column(db.String(512), nullable=False)
    def as_dict(self):
       return {c.name: getattr(self, c.name) for c in self.__table__.columns}
